package com.xyzboom.compiler.compiler.grammar

import com.xyzboom.compiler.NoArg

/**
 * Production
 *
 * @constructor Create empty Production
 */
@NoArg
class Production {
    /**
     * Left
     *
     * Non-terminator at left of the production
     */
    val left: NonTerminator

    /**
     * Right of the production
     */
    val right: ArrayList<ISymbol>
    operator fun get(index: Int) =
        right[index]

    constructor(left: NonTerminator, right: ArrayList<ISymbol>) {
        this.left = left
        this.right = right
    }

    constructor(left: NonTerminator) {
        this.left = left
        this.right = ArrayList()
    }

    override fun toString(): String {
        return "Prod\t${left.name} --> ${right.joinToString { it.simpleString() }}"
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is Production) return false

        if (left != other.left) return false
        if (right != other.right) return false

        return true
    }

    override fun hashCode(): Int {
        var result = left.hashCode()
        result = 31 * result + right.hashCode()
        return result
    }

}